package com.soosync.common;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import javax.servlet.http.*;

import org.json.JSONException;
import org.json.JSONObject;

public class CollectionUtility
{

 private CollectionUtility()
 {
 }

 public static String decode(String s)
 {
     String decodedString = null;
     try
     {
         decodedString = URLDecoder.decode(s);
     }
     catch(Exception _ex) { }
     return decodedString;
 }

 public static String encode(String s)
 {
     return URLEncoder.encode(s);
 }

 public static JSONObject getAttributeBox(HttpServletRequest req)
 {
	 JSONObject box = new JSONObject();
     String key;
     try {
		for(Enumeration e = req.getAttributeNames(); e.hasMoreElements(); box.put(key, req.getAttribute(key)))
		     key = (String)e.nextElement();
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    return box;
 }

 public static JSONObject getJSONBox(HttpServletRequest req)
 {
	 JSONObject box = null;
	try {
		box = new JSONObject();
     String key;
     for(Enumeration e = req.getParameterNames(); e.hasMoreElements(); box.put(key, CharsetManager.IsoToUTF8(req.getParameter(key))))
         key = (String)e.nextElement();
	} catch (JSONException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
     return box;
 }
 public static JSONObject getBoxFromCookie(HttpServletRequest req)
 {
	 JSONObject cookiebox = new JSONObject();
     Cookie cookies[] = req.getCookies();
     if(cookies == null)
         return cookiebox;
     for(int i = 0; cookies != null && i < cookies.length; i++)
     {
         String key = cookies[i].getName();
         String value = cookies[i].getValue();
         if(value == null)
             value = "";
         String values[] = new String[1];
         values[0] = value;
         try {
			cookiebox.put(key, values);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     }

     return cookiebox;
 }

 public static JSONObject getNewSessionBox(HttpServletRequest req)
 {
     return getNewSessionBox(req, "shared sessionbox");
 }

 public static JSONObject getNewSessionBox(HttpServletRequest req, String name)
 {
     HttpSession session = req.getSession(true);
     JSONObject sessionbox = null;
     if(!session.isNew())
     {
         Object o = session.getAttribute(name);
         if(o != null)
             session.removeAttribute(name);
     }
     sessionbox = new JSONObject();
     session.setAttribute(name, sessionbox);
     String key;
     try {
		for(Enumeration e = req.getParameterNames(); e.hasMoreElements(); sessionbox.put(key, req.getParameterValues(key)))
		     key = (String)e.nextElement();
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

     return sessionbox;
 }

 public static JSONObject getSessionBox(HttpServletRequest req)
 {
     return getSessionBox(req, "shared sessionbox");
 }

 public static JSONObject getSessionBox(HttpServletRequest req, String name)
 {
     HttpSession session = req.getSession(true);
     JSONObject sessionbox = null;
     Object o = session.getAttribute(name);
     if(o != null)
         if(o instanceof JSONObject)
             sessionbox = (JSONObject)o;
         else
             session.removeAttribute(name);
     if(sessionbox == null)
     {
         sessionbox = new JSONObject();
         session.setAttribute(name, sessionbox);
     }
     String key;
     try {
		for(Enumeration e = req.getParameterNames(); e.hasMoreElements(); sessionbox.put(key, req.getParameterValues(key)))
		     key = (String)e.nextElement();
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

     return sessionbox;
 }
}


